package com.liuchuanzheng.okhttpinterceptordemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.liuchuanzheng.okhttpinterceptordemo.interceptor.FinalInterceptor;
import com.liuchuanzheng.okhttpinterceptordemo.interceptor.Interceptor;
import com.liuchuanzheng.okhttpinterceptordemo.interceptor.RealInterceptorChain;
import com.liuchuanzheng.okhttpinterceptordemo.interceptor.Request;
import com.liuchuanzheng.okhttpinterceptordemo.interceptor.Response;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_request = findViewById(R.id.btn_request);
        btn_request.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request();
            }
        });
    }

    private void request() {
        //1.构造Request
        //2.构造拦截器容器
        //3.添加内部的或外部的拦截器
        //4.构造第一个Chain对象，交给第一个拦截器，发起责任两从上到下的处理。
        //5.拿到处理后的Response

        //1
        Request request = new Request();
        request.url = "http://www.baidu.com";

        //2
        List<Interceptor> interceptors = new ArrayList<>();

        //3 这里用户可以添加多个拦截器,会按顺序 request 1,2,3... response ...3,2,1  这种顺序类似Android的事件分发

        interceptors.add(new HeaderInterceptor());
        interceptors.add(new LogInterceptor());

        interceptors.add(new FinalInterceptor());

        //4
        //创建第一个节点
        RealInterceptorChain chain = new RealInterceptorChain(interceptors, 0, request);
        //第一个节点执行,目的是索要一个response,索要的同时,就把原始request传走了,传给了责任链的下个节点.那么下个节点关联的Interceptor
        // 就会执行intercept方法.里边还是索要response,最终,索要到FinalInterceptor时,response就new出来了.然后倒序retun,最终return到这里
        Response response = chain.proceed(request);

        //5
        Log.i("okhttp","最终拿到response:"+response.toString());
    }
    //用户自定义的log拦截器
    public class LogInterceptor implements Interceptor{
        @Override
        public Response intercept(Chain chain) {
            Log.i("okhttp","LogInterceptor开始执行");
            Request request = chain.request();
            Log.i("okhttp","request:"+request.toString());
            Response response = chain.proceed(request);
            Log.i("okhttp","response:"+response.toString());
            Log.i("okhttp","LogInterceptor拿到response并传递:"+response.toString());
            return response;
        }
    }
    //用户自定义的header拦截器
    public class HeaderInterceptor implements Interceptor{
        @Override
        public Response intercept(Chain chain) {
            Log.i("okhttp","HeaderInterceptor开始执行");
            Request request = chain.request();
            request.header = "自定义header";
            Response response = chain.proceed(request);
            Log.i("okhttp","HeaderInterceptor拿到response并传递"+response.toString());
            return response;
        }
    }
}